(ns ref.grid)

(defn create-ref-grid [width height content-fn]
  (map 
    (fn [_] (map
              (fn [_] (ref content-fn))
              (range height))) 
    (range width)))

(defn width [grid]
  (count grid))

(defn height [grid]
  (count (first grid)))

(defn ref-at [grid x y]
  (nth (nth grid x) y))

(defn val-at [grid x y]
  @(ref-at grid x y))

(defn every-cell-in-range 
  ([max-x max-y callback] 
    (every-cell-in-range 0 max-x 0 max-y callback))
  ([min-x max-x min-y max-y callback]
    (for [x (range min-x max-x)
          y (range min-y max-y)]
      (callback x y))))

(defn every-value? [predicate game]
  (every?
    predicate
    (map (fn [arg] @arg) (apply concat game))))